#!/usr/bin/python

import gobject;
gobject.threads_init()
import gst;

if __name__ == "__main__":
    # First create our pipeline
    pipe = gst.Pipeline("mypipe")

    # Create a software mixer with "Adder"
    adder = gst.element_factory_make("adder","audiomixer")
    audioconvert = gst.element_factory_make("audioconvert","audioconvert")

    output = gst.element_factory_make("pulsesink", "audio_out")

    #Create microphone
    microphone = gst.element_factory_make("pulsesrc","microphone")
    microphone.set_property("device","alsa_input.pci-0000_02_02.0.analog-stereo")

    pipe.add(microphone, adder, audioconvert,  output)
    gst.element_link_many(microphone, adder, audioconvert,  output)


    #Create the music 
    source = gst.element_factory_make("filesrc", "file-source")
    source.set_property("location", "/mnt/c_drive/test.mp3")
    decoder = gst.element_factory_make("mad", "mp3-decoder")
    songaudioconvert = gst.element_factory_make("audioconvert", "songaudioconvert")
    volume_control = gst.element_factory_make("volume", "volume_control")
    volume_control.set_property("volume", 0.01)
    pipe.add(source, decoder, songaudioconvert, volume_control)
    gst.element_link_many(source, decoder, songaudioconvert, volume_control, adder)
    
    
    
    
    #start the sound
    pipe.set_state(gst.STATE_PLAYING)
    
    
    raw_input("Add second sound Press <ENTER> to continue.")
    volume_control.set_property("volume", 0.5)
    
    raw_input("Add second sound Press <ENTER> to continue.")
    volume_control.set_property("volume", 1)


    raw_input("Add second sound Press <ENTER> to continue.")
    source.unlink(decoder)
    source2 = gst.element_factory_make("filesrc", "file-source2")
    source2.set_property("location", "/mnt/c_drive/brothers.mp3")
    pipe.add(source2)
    source2.link(decoder)
    
    volume_control.set_property("volume", 1)
    
    
    pipe.set_state(gst.STATE_PLAYING)
    
    


    raw_input("1kHz test sound. Press <ENTER> to continue.")
    
    # Create the second buzzer..
    buzzer2 = gst.element_factory_make("audiotestsrc","buzzer2")
    buzzer2.set_property("freq",100)
    buzzer2.set_property("volume", 0.6)
    
    pipe.add(buzzer2)
    
    # Get an another request sink pad on the mixer
    sinkpad2=adder.get_request_pad("sink%d")
    
    buzzersrc2=buzzer2.get_pad("src")
    buzzersrc2.link(sinkpad2)

    pipe.set_state(gst.STATE_PLAYING)

    raw_input("1kHz test sound. Press <ENTER> to continue.")
    
    
    buzzersrc2=buzzer2.get_pad("src")
    buzzersrc2.set_blocked(True)
    # Stop the first buzzer
    buzzer2.set_state(gst.STATE_NULL)
    # Unlink from the mixer
    buzzersrc2.unlink(sinkpad2)
    # Release the mixers first sink pad
    adder.release_request_pad(sinkpad2)
    
    # Because here none of the Adder's sink pads block, streaming continues
    pipe.set_state(gst.STATE_PLAYING)
    
    raw_input("1kHz test sound. Press <ENTER> to continue.")
    
    
    